﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.DataAccess;
using University.Workshop.Model;

namespace University.Workshop.Business
{
    public class ManagerExpense
    {
        public static void NewExpense(Model.Expense newExpense)
        {
            newExpense.DateRegistry = DateTime.Now;

            DataAccessExpense.CreateExpense(newExpense);
        }

        public static void UpdateExpense(Model.Expense modifyExpense)
        {

            DataAccessExpense.UpdateExpense(modifyExpense);

        }

        public static void VerifyDataExpense(Model.Expense ex)
        {
            if (ex.Amount.Equals(0))
                throw new Exception("La cantidad de items es un dato obligatorio.");
            if (ex.Price.Equals(0))
                throw new Exception("El monto de los items es un dato obligatorio.");
            if (ex.ExpenseType.Equals(-1))
                throw new Exception("Es necesario seleccionar un item.");
            if (ex.SpendDate > DateTime.Today)
                throw new Exception("Es necesario seleccionar una fecha válida.");
        }

        public static ICollection<Model.Expense> SearchExpenseByParameter(Model.Expense purchasetoFind)
        {
            ICollection<Model.Expense> u;
            u = DataAccessExpense.SearchExpenseByParameterAll(purchasetoFind);

            if (u.Count == 0)
            {
                throw new Exception("No se encontraron resultados de la busqueda...");
            }

            foreach (Model.Expense expense in u)
            {
                expense.estructura = ((ExpenseType)expense.ExpenseType).ToString();
            }
            return u;
        }


        public static ICollection<Model.Expense> SearchExpenseMinusDate(Model.Expense purchasetoFind)
        {
            ICollection<Model.Expense> u;
            u = DataAccessExpense.SearchExpenseMinusDatePurchase(purchasetoFind);

            if (u.Count == 0)
            {
                throw new Exception("No se encontraron resultados de la busqueda...");
            }
            foreach (Model.Expense expense in u)
            {
                expense.estructura = ((ExpenseType)expense.ExpenseType).ToString();
            }
            return u;
        }

        public static Model.Expense GeExpenseByReceipt(Model.Expense waterPurchase)
        {
            var purchases = DataAccessExpense.GetExpenseByReceipt(waterPurchase);
            if (purchases.Count == 0)
                return null;
            else
                return purchases.First();
        }

        public static Model.Expense GetExpenseById(DateTime date)
        {
            ICollection<Model.Expense> expenseList = DataAccessExpense.GetExpenseById(date);
            if (expenseList.Count == 0)
            {
                return new Model.Expense();
            }
            return expenseList.First();
        }
    }

}
